package com.stone.starter.core.util.exception;

import com.stone.starter.core.exception.CommonException;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;

/**
 * 异常工具类，将异常堆栈完整的转换为字符串，方便存储异常堆栈
 *
 * @author Mr_wenpan@163.com 2021/12/1 11:29 上午
 */
public class ExceptionUtils {

    private ExceptionUtils() {
    }

    public static String getMessage(Exception e) {
        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
             PrintStream pout = new PrintStream(out, false, StandardCharsets.UTF_8.displayName())) {
            e.printStackTrace(pout);
            return new String(out.toByteArray(), StandardCharsets.UTF_8);
        } catch (Exception ex) {
            throw new CommonException(ex);
        }
    }
}